package com.hlhj.funlike_businessqzxd.utils;

import android.app.DialogFragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.hlhj.funlike_businessqzxd.R;

public class MyFoodDialog extends DialogFragment {

    private View view;
    private TextView tv_cancle,tv_ok;
    private EditText et_addguige;
    private String string="";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NO_TITLE, R.style.MyDialog);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        view = View.inflate(getActivity(),R.layout.dialog_food,null);
        initView();
        return view;
    }

    private void initView() {
        et_addguige=view.findViewById(R.id.et_addguige);
        tv_cancle=view.findViewById(R.id.tv_cancle);
        tv_ok=view.findViewById(R.id.tv_ok);

        et_addguige.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if (charSequence.toString().length()>=6){
                    Toast.makeText(getActivity(),"最多输入6个字",Toast.LENGTH_SHORT).show();
                }
                string=charSequence.toString();
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

        tv_cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });

        tv_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (listener!=null){
                    String str = string.replace(" ", "");
                    if (TextUtils.isEmpty(str)){
                        return;
                    }
                    listener.showGuiGe(str);
                }
                et_addguige.setText("");
                dismiss();
            }
        });
    }

    @Override
    public void onStart(){
        super.onStart();
        Window window = getDialog().getWindow();
        WindowManager.LayoutParams lp= window.getAttributes();
        lp.dimAmount = 0.2f;
        lp.flags |= WindowManager.LayoutParams.FLAG_DIM_BEHIND;
        window.setAttributes(lp);
    }

    public interface AddFood{
        void showGuiGe(String msg);
    }

    public AddFood listener;

    public void setListener(AddFood listener) {
        this.listener = listener;
    }
}
